//action.js
//获取应用实例
var app = getApp()
var httpUtil = require("../../utils/http.js");

Page({
  data: {
    hidden: false,
    list: [],
    current:1,
    size:10,
    scrollTop: 0,
    scrollHeight: 0,
    triggered:false,
    isLastPage: false, // 是否是最后一页
  },
  
  onLoad: function () {
    //注意 scroll-view 必须要设置高度才能监听滚动事件，需要在页面的onLoad事件中给scroll-view的高度赋值
    var that = this;
    wx.getSystemInfoAsync({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
        that.loadData("loadMore")
      }
    });
  },

  //上拉加载
  bindDownLoad: function () {
    console.log("bindDownLoad===1111111111111111")
    this.loadData("loadMore");
  },
  //下拉刷新
  bindRefresh: function () {
    console.log("bindRefresh===1111111111111111")
    //数据刷新
    this.setData({
      current: 1,
      isLastPage: false,
      triggered: true,
    });
    this.loadData("refresh");
  },
  
  //加载更多
  loadData:function (loadType) {
    if(this.data.isLastPage){
       return;
    }
    let that = this;
    const params = {
      url:  '/pic/getActionPictures' ,
      method: "POST",
      data: {
        current: that.data.current,
        size: that.data.size,
      },
      callBack: (res) => {
        if(loadType && loadType=='refresh'){
          that.setData({
            list: []
          });
        }
        let list = that.data.list ;
        let records = res.data.records;
        let totalPage = res.data.pages;
        for(var i = 0; i < records.length; i++){
          list.push(records[i]);
        }
        if( totalPage>that.data.current){
          that.setData({
            list: list,
            current:that.data.current+1,
            isLastPage: false
          });
        }else{
          that.setData({
            list: list,
            isLastPage: true
          });
        }
        console.log("loadType",loadType)
        if(loadType && loadType=='refresh'){
          console.log("refresh")
          that.setData({
            triggered:false,
          });
        }
      }
    };
    httpUtil.request(params);
  },
})